/**
 * 项目：ystree
 * 模块：树操作。生成树的各种表现形式（html / xml / ...）
 * 作者：yangjianliang
 * 日期：2007-6-22
 */
package net.ysen.tree.impl;

import java.util.ArrayList;
import java.util.List;

import net.ysen.tree.Consts;
import net.ysen.tree.abst.TreeAbstract;
import net.ysen.tree.entitybean.TreeBean;

public class YstreeImpl extends TreeAbstract {

	private int spaceNum = 0;
	private int spaceNumAddStep = 2;
//	private String spaceflag = "&nbsp;";
	private String xmlspaceflag = " ";
	private String spaceflag = "<span width=\"20px\">&nbsp;&nbsp;&nbsp;</span>";
	
	public List<TreeBean> treeBeanConversion(List list){
		List<TreeBean> sons = new ArrayList<TreeBean>();
		for(int i=0,j=list.size();i<j;i++){
			Object[] o = (Object[])list.get(i);
			TreeBean treeson = new TreeBean();
			treeson.setId(o[1].toString());
			treeson.setParentid(o[2].toString());
			treeson.setText(o[0].toString());
			treeson.setValue(o[3].toString());
			sons.add(treeson);
		}
		return sons;
	}

	/* (non-Javadoc)
	 * @see net.ysen.tree.ITree#getTreeHtml()
	 */
	public String getTreeHtml(TreeBean tree) {

		if(tree==null||tree.getId()==null)
			return "";

		StringBuffer html = new StringBuffer("");
		html.append("<div id=\"ystreeDiv_").append(tree.getId()).append("\" onMouseOver=\"colorChange(this);\" onMouseOut=\"colorBack(this);\">");
		html.append(lineSeparator);
		for(int i =0;i<spaceNum;i++){
			if(i == 0){
				html.append(spaceflag);
			}else{
				if( tree.isEndNode() ){
					html.append("<img src='"+Consts.defaultStylePath + Consts.frontImage_noChildrenLastNode+"' border='0'>");
				} else{
					html.append("<img src='"+Consts.defaultStylePath + Consts.frontImage_verticalLine+"' border='0'>");
				}
			}
		}
		
		html.append("<img id=\"ystreeNodeImg_").append(tree.getId()).append("\" name=\"ystreeNodeSwitch\" src=\"");
		if(tree.getSonNode()==null || tree.getSonNode().isEmpty()){
			//没有子节点,表示自己就是最底层了
			if( tree.isFootNode() ){
				html.append(Consts.defaultStylePath).append(Consts.frontImage_noChildrenLastNode);
			}else{
				html.append(Consts.defaultStylePath).append(Consts.frontImage_noChildrenMidNode);
			}
			html.append("\" border=\"0\"/>");
		}else{
			if(tree.isRootNode() == false){
				if(tree.isExpandNode()){
					html.append(Consts.defaultStylePath).append(Consts.frontImage_expandedMidNode);
				}else{
					html.append(Consts.defaultStylePath).append(Consts.frontImage_collapsedMidNode);
				}
			}else{
				if(tree.isExpandNode()){
					html.append(Consts.defaultStylePath).append(Consts.frontImage_expandedTopNode);
				}else{
					html.append(Consts.defaultStylePath).append(Consts.frontImage_collapsedTopNode);
				}
			}
			html.append("\" border=\"0\" onclick=\"displayNode(this);\"/>");
		}
		if( TreeBean.isShowCheckBox() ){
			html.append("<input id=\"").append(tree.getId()).append("\" name=\"ystreeCheckBoxName\" "); 
			html.append( tree.isChecked()?"checked ":null );
			html.append("value=\"").append(tree.getValue()).append("\" type=\"checkbox\" onClick=\"resetValue(this);\">");
		}else{
			html.append("&nbsp;");
		}
		html.append("<label for=\"").append(tree.getId()).append("\" id=\"label_").append(tree.getId()).append("\" onClick=\"clickingText(this);\">");
		html.append(tree.getText());
		html.append("</label>").append(lineSeparator);
		html.append("</div>").append(lineSeparator);
		if(tree.isExpandNode() && tree.hasSonNode()){
			html.append("<div id=\"ystreeDivPane_").append(tree.getId()).append("\">").append(lineSeparator);
			html.append( getTreeSonHtml( tree.getSonNode() ) );
			html.append("</div>").append(lineSeparator);
		}
		return html.toString();
	}

	protected String getTreeSonHtml(List<TreeBean> treeNodes){
		if(treeNodes==null||treeNodes.isEmpty())
			return "";
		spaceNum += spaceNumAddStep;//缩进多少格
		int d = spaceNum;//记录下此时的空格数
		StringBuffer html = new StringBuffer("");
		for(int i=0,k=treeNodes.size();i<k;i++){
			TreeBean bean = (TreeBean) treeNodes.get(i);
			if( i==k-1 )bean.setFootNode(true);
			html.append( getTreeHtml( bean ) );
			spaceNum = d;//恢复进入前的空格数
		}
		/*
		for(Iterator it = treeNodes.iterator();it.hasNext();){
			TreeBean bean = (TreeBean) it.next();
			html.append( getTreeHtml( bean ) );
			if(it.hasNext()==false)bean.setFootNode(true);
			spaceNum = d;//恢复进入前的空格数
		}
		*/
		return html.toString();
	}

	/*
	 * (non-Javadoc)
	 * @see net.ysen.tree.abst.TreeAbstract#getTreeXML(net.ysen.tree.entitybean.TreeBean)
	 */
	public String getTreeXML(TreeBean tree){

		if(tree==null||tree.getId()==null)
			return "";

		StringBuffer xmlstr = new StringBuffer("");
//		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append(lineSeparator);
		//类型在返回该XML得页面设置消息头中设置，这里不能再有，有些游览器报错。
//		xml.append("<ystree>").append(lineSeparator);
		getTreeNodeXML(tree, xmlstr);
//		xml.append("</ystree>").append(lineSeparator);
		
		return xmlstr.toString();
	}
	
	protected String getTreeNodeXML(TreeBean tree, StringBuffer xml){

		if(tree==null||tree.getId()==null)
			return xml.toString();

		for(int i =0;i<spaceNum;i++){
			xml.append( xmlspaceflag );	
		}
		xml.append("<treenode id=\""+ tree.getId() +"\" parentid=\""
				+ tree.getParentid() 
				+"\" name=\""+ tree.getName() +"\" value=\""+ tree.getValue() +"\" ");
		xml.append("text=\""+tree.getText() + "\" ");
		xml.append("childNodesAmount=\""+tree.getChildNodesAmount() + "\" >");
		if( tree.hasSonNode()){
			xml.append(lineSeparator);
			xml.append( getTreeSonXml( tree.getSonNode() ) );
			//恢复当前节点的缩进
			int a = (spaceNum -= spaceNumAddStep*2);
			for(int i =0;i<a;i++){
				xml.append( xmlspaceflag );
			}
		}
		xml.append("</treenode>").append(lineSeparator);
		return xml.toString();
	}
	
	protected String getTreeSonXml(List<TreeBean> treeNodes){
		if(treeNodes==null||treeNodes.isEmpty())
			return "";
		spaceNum += spaceNumAddStep*2;//缩进多少格
		int d = spaceNum;//记录下此时的空格数
		StringBuffer xml = new StringBuffer("");
		for(int i=0,k=treeNodes.size();i<k;i++){
			TreeBean bean = (TreeBean) treeNodes.get(i);
			if( i==k-1 )bean.setFootNode(true);
			getTreeNodeXML( bean, xml );
			spaceNum = d;//恢复进入前的空格数
		}
		
		return xml.toString();
	}
}
